<template>
  <div>
    <el-form class="input-form"
             label-position="right"
             label-width="120px"
             ref="editForm"
             :rules="propRules"
             :disabled="propReadonly"
             :model="form"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="收货科室" prop="deptId">
            <el-select v-model="form.deptId" filterable>
              <el-option value="">请选择</el-option>
              <el-option v-for="dict in propDictionaries['deptId']" :key="dict.id" :label="dict.name"
                         :value="dict.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="业务单号" prop="busKey">
            <el-input v-model="form.busKey" disabled placeholder="自动设置"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="配送日期" prop="busDate">
            <el-date-picker v-model="form.busDate" type="date" :format="dates.defaultDatePattern"
                            :picker-options="dates.pickerOptions.date"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配送人" prop="busUser">
            <el-input v-model="form.busUser"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="配送仓库" prop="warehouseId">
            <el-select v-model="form.warehouseId" filterable :disabled="strings.isNotBlank(form.applyId)">
              <el-option value="">请选择</el-option>
              <el-option v-for="dict in propDictionaries['warehouseId']" :key="dict.id" :label="dict.name"
                         :value="dict.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="拣货人" prop="picker">
            <el-input v-model="form.picker"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="请领日期" prop="applyDate">
            <el-date-picker v-model="form.applyDate" type="date" :format="dates.defaultDatePattern"
                            :picker-options="dates.pickerOptions.date" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请领单号" prop="applyKey">
            <el-input v-model="form.applyKey" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>

        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="3"/>
          </el-form-item>
        </el-col>
      </el-row>


      <div class="form-unit" v-show="propStatus==='edit'">拣货操作</div>
      <el-row :gutter="20" class="search-box" v-show="propStatus==='edit'">
        <el-col :span="24">
          <el-form-item label="条码" prop="txtScan">
            <el-input v-model="txtScan" placeholder="在此设置光标，开始扫描SN/RFID/UDI进行拣货"  @keyup.native.enter="pick"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" class="search-box"   v-show="propStatus==='edit'">
        <el-col :span="24">
          <el-form-item label="" prop="txtScan">
            <el-collapse>
              <el-collapse-item title="拣货明细" name="1">
                <data-view v-show="propStatus==='edit'"
                           propViewKey="wm_pick_item"
                           :propInitData="form"
                           ref="pickItemDataView"
                           :propHidePageSearch="true"
                           :propHideQueryButton="true"
                           :propAutoFetchData="false"
                />
              </el-collapse-item>
            </el-collapse>
          </el-form-item>
        </el-col>
      </el-row>

      <div class="form-unit">配送单明细</div>
      <el-row>
        <div style="height:500px;overflow:auto;">
          <data-view v-show="propStatus==='edit'"
                     propViewKey="wm_dist_item"
                     propViewForm="wm/dist-item/detail.vue"
                     :propInitData="form"
                     ref="childDataView"
                     :propHidePageSearch="true"
                     :propHideQueryButton="true"
                     :propAutoFetchData="false"
          />
        </div>
      </el-row>

    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      form: {},
      viewData: {},
      txtScan: '',
    }
  },
  methods: {
    getChildViewQueryWrapper() {
      if (this.form.id) {
        return this.$newWrapper().eq('distId', this.form.id)
      } else {
        return this.$newWrapper().ne('1', 1)
      }
    },
    pick() {
      if (this.strings.isBlank(this.txtScan)) {
        return
      }
      this.$ajax.post('/wm/dist/pick', { distId: this.form.id, uniqueCode: this.txtScan}).then(resp => {
        this.utilMessage.success(resp.msg)

        let wrapper = this.getChildViewQueryWrapper()
        this.$refs.childDataView.setQueryWrapper(wrapper)
        this.$refs.pickItemDataView.setQueryWrapper(wrapper)
      })
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)

        let wrapper = this.getChildViewQueryWrapper()
        this.$refs.childDataView && this.$refs.childDataView.setQueryWrapper(wrapper)
        this.$refs.pickItemDataView && this.$refs.pickItemDataView.setQueryWrapper(wrapper)
      },
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  components: {},
  computed: {
    cpChildRows() {
      if (!this.$refs.childDataView) {
        return []
      }
      return this.$refs.childDataView.getRows0()
    }
  },
  watch: {

  }
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100%;
  }
</style>
